//
//  TitleButton.swift
//  Practice
//
//  Created by miaozhihui on 2017/2/6.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import UIKit

class TitleButton: UIButton {
    
    /// 重载构造函数
    /// title 如果为nil 显示 首页
    /// - Parameter title: title
    init(title:String?) {
        super.init(frame: CGRect.zero)
        if title == nil {
            setTitle("首页", for: .normal)
        } else {
            setTitle(title!+" ", for: .normal)
            setImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel?.bounds.width ?? 0
    }
}
